<?php
// protected/components/helpers.php
 
 /*
  *  helper function for paging in fql results
  *  as a first approach it is assumed that number of items per page = 5
  */
function paging($link, $page, $obj=array())
{
		$z = 0;
        foreach($obj as $item){
        	$z=$z+1;
        }
        $next = $page + 5;
        $prev = $page - 5;
        $seite = ($page/5) + 1;
        $return = "Seite ".$seite;
        if($prev >= 0) {
        	$return .= '<a href="'.$link.$prev .'"> | zurück</a>'; 
        	if($z >= 5) $return .= " | ";
        } else $return .= " | ";
		if($z >= 5) $return .= '<a href="'.$link.$next.'">weiter </a><br />'; 
		
		return $return;
}
function pagingnew($link, $page, $obj=array())
{
		$z = 0;
        foreach($obj as $item){
        	$z=$z+1;
        }
        $next = $page + 5;
        $prev = $page - 5;
        $seite = ($page/5) + 1;
        $return = "Seite ".$seite;
        if($prev >= 0) {
        	$return .= CHtml::ajaxLink(
  " | zurück",
  yii::app()->createUrl( 'offer/a' ),
  array( // ajaxOptions
    'type' => 'POST',
    'update'=>'#_ajax',
    'beforeSend' => 'function( request )
                     {
                       $("#myDiv").addClass("loador");
                     }',
    'complete' => 'function( data )
                  {
                    $("#myDiv").removeClass("loadors");
                  }',
    'data' => array( 'val1' => $prev, 'val2' => '2' )
  ),
  array( //htmlOptions
    'href' => Yii::app()->createUrl( 'offer/a'), //'confirm' => 'Are you sure?',
    'class' => 'jjj'
  )
); 
        	if($z >= 5) $return .= " | ";
        } else $return .= " | ";
		if($z >= 5) $return .= CHtml::ajaxLink(
  " | weiter",
  yii::app()->createUrl( 'offer/a' ),
  array( // ajaxOptions
    'type' => 'POST',
    'update'=>'#_ajax',
    'beforeSend' => 'function( request )
                     {
                       $("#myDiv").addClass("loador");
                     }',
    'complete' => 'function( data )
                  {
                    $("#myDiv").removeClass("loador");
                  }',
    'data' => array( 'val1' => $next, 'val2' => '2' )
  ),
  array( //htmlOptions
    'href' => Yii::app()->createUrl( 'offer/a'), //'confirm' => 'Are you sure?',
    'class' => 'jjj'
  )
).'<br />'; 
		
		return   $return;

}


	function get_url($URL,$decode = 0){
	          	$ch = curl_init();
          	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
          	curl_setopt($ch, CURLOPT_URL, $URL);
          	$data = curl_exec($ch);
          	curl_close($ch);
			//$result = get_content($url);
			//$data = json_decode($data);
			if ($decode == 0)
				return json_decode($data);
			else return $data;
			}
			
 /*
  *  get Location information from google based upon latitude, longitude
  *  
  */
      function get_distance($lat, $lon, $lat2, $lon2){
      		$URL = "http://maps.googleapis.com/maps/api/distancematrix/json?origins=".$lon.",".$lat."&destinations=".$lon2.",".$lat2."&language=de-DE&sensor=false";
      		//$URL = "http://maps.googleapis.com/maps/api/distancematrix/json?origins=Vancouver+BC|Seattle&destinations=San+Francisco|Victoria+BC&mode=bicycling&language=de-DE&sensor=false";
      		//$URL = "http://maps.google.com/maps/geo?q=".$lon.','.$lat."&output=json&key=ABQIAAAAhdDokCu8GspRD8rZaYYaoRR5dndtY5oS-GFuRoRAautbbsCzexTbXFh0ItLJ_JAtzGaD1Nh8I-kRzQ";
			$data = get_url($URL, 1);
			return $data;
      }	
			
		
 /*
  *  get Location information from google based upon latitude, longitude
  *  
  */
      function get_location($lat, $lon){
      		$URL = "http://maps.google.com/maps/geo?q=".$lon.','.$lat."&output=json&key=ABQIAAAAhdDokCu8GspRD8rZaYYaoRR5dndtY5oS-GFuRoRAautbbsCzexTbXFh0ItLJ_JAtzGaD1Nh8I-kRzQ";
			$data = get_url($URL, 0);
			return $data;
      }
      
 /*
  *  get Location information from google based upon latitude, longitude
  *  
  */
    function get_coordinates($location){    
    	$place = str_replace(" ", "+", $location);  
		$URL = "http://maps.google.com/maps/geo?output=json&q=".$place;
		
		$data = get_url($URL);
		$point = $data->Placemark[0]->Point->coordinates;
		return array('latitude'=>$point[0], 'longitude'=>$point[1]);
	}